package com.atguigu.aclservice.controller;


import com.atguigu.aclservice.entity.User;
import com.atguigu.aclservice.entity.vo.PasswordVo;
import com.atguigu.aclservice.service.UserService;
import com.atguigu.commonutils.MD5Utils;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author fxy
 * @since 2022-10-27
 */
@Api(tags = "用户管理接口")
@Slf4j
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/acl/user")
public class UserController {

    @Autowired
    private UserService userService;

//    @Autowired
//    private SysLoginLogService sysLoginLogService;

//    @Log(title = "更改用户状态",businessType = BusinessType.UPDATE)
    @ApiOperation("更改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id,
                               @PathVariable Integer status) {
        userService.updateStatus(id,status);
        return R.ok();
    }

//    @Log(title = "用户列表",businessType = BusinessType.FIND)
    @ApiOperation("用户列表")
    @PostMapping("/{page}/{size}")
    public R list(@PathVariable Long page,
                       @PathVariable Long size,
                       @RequestParam(required = false,value = "userName")String userName) {

        Page<User> roomPage=new Page<>(page,size);
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();


        if (userName!=null){
            wrapper.like(User::getUsername,userName);
        }

        wrapper.orderByDesc(User::getCreateTime);


        userService.page(roomPage,wrapper);
        List<User> userList = roomPage.getRecords();
        long total = roomPage.getTotal();
        return R.ok().data("userList",userList).data("total",total);
    }

//    @Log(title = "添加用户",businessType = BusinessType.INSERT)
    @ApiOperation("添加用户")
    @PostMapping("save")
    public R save(@RequestBody User user) {
        user.setHeadUrl("http://gulistudydemo.oss-cn-beijing.aliyuncs.com/2022/12/16/56812d8cb7154665aaf45ea1a60537ed2020-10-09 154632.jpg");
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        User one = userService.getOne(wrapper);
        if (one!=null){
            throw new GuliException(20001,"该用户名已存在，请更换！！！");
        }
        //把输入密码进行加密 MD5
        String encrypt = MD5Utils.md5(user.getPassword());
        user.setPassword(encrypt);
        boolean is_Success = userService.save(user);
        if(is_Success) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//    @Log(title = "根据id查询用户信息",businessType = BusinessType.FIND)
    @ApiOperation("根据id查询用户信息")
    @GetMapping("getUser/{id}")
    public R getUser(@PathVariable Long id) {
        User user = userService.getById(id);
        return R.ok().data("user",user);
    }

//    @Log(title = "修改用户信息",businessType = BusinessType.UPDATE)
    @ApiOperation("修改用户信息")
    @PostMapping("update")
    public R update(@RequestBody User user) {
        boolean is_Success = userService.updateById(user);
        if(is_Success) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//    @Log(title = "删除用户",businessType = BusinessType.DELETE)
    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        boolean is_Success = userService.removeById(id);
        if(is_Success) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}

